<?php

    $posts = new posts_class();

    class ticket_class
    {
        public $id = null;
        public $time = null;
        public $status = null;
        function __construct($id, $time, $status)
        {
            $this->id = $id;
            $this->time = $time;
            $this->status = $status;
        }
    }

    class workdate_class
    {
        public $date = null;
        public $timebegin = null;
        public $timeend = null;
        public $place = null;
        public $tickets = Array();
        function __construct($date, $timebegin, $timeend, $place)
        {
            $this->date = $date;
            $this->timebegin = $timebegin;
            $this->timeend = $timeend;
            $this->place = $place;
        }
    }

    class post_class
    {
        public $id = null;
        public $fio = null;
        public $specialityname = null;
        public $spacialityid = null;
        public $state = null;
        public $division = null;
        public $workdates = Array();
    }

    class posts_class
    {

        public $posts_items = Array();

        public function items()
        {
            return $this->posts_items;
        }
        
        public function __construct()
        {
            if(!isset($_SESSION['form'])){return false;}
            global $auth;
            $form_values = $_SESSION['form'];
            $need_for_speciality = Array();
            if(isset($form_values['dict']['spec']))
            {
                foreach($form_values['dict']['spec'] as $key => $value)
                {
                    if($value!="")
                    {
                        $need_for_speciality[] = $value;
                    }
                }
            }
            $buf = $this->getdata(null, $need_for_speciality, constant('DATEBEGIN'), constant('DATEEND'));
            $this->parsexml($buf);
        }

        public function count()
        {
            return count($this->posts_items);
        }

        public function add($id, $fio, $profile, $specialityname, $spacialityid, $state, $division)
        {
            $post = new post_class();
            $post->id = $id;
            $post->fio = $fio;
            $post->profile = $profile;
            $post->specialityname = $specialityname;
            $post->spacialityid = $spacialityid;
            $post->state = $state;
            $post->division = $division;
            $this->posts_items[$specialityname][] = $post;
            return $post;
        }

        public function add_ticket($post_item, $id, $date, $time, $status, $place)
        {
            $ticket = new ticket_class();
            $ticket->id = $id;
            $ticket->date = $date;
            $ticket->time = $time;
            $ticket->status = $status;
            $ticket->place = $place;
            $post_item->tickets[] = $ticket;
        }

        public function getdata($server, $need_for_speciality, $datebegin, $dateend, $need_for_posts = null)
        {
            $xml = CreateGetScheduleXML($need_for_speciality, $datebegin, $dateend, $need_for_posts);  
            $buf = CallSoap('posts', $xml, $server);
            return $buf;
        }

        public function parsexml($buf)
        {
            $dom1 = new DomDocument();
            $dom1->loadXML($buf);
            $posts_elm = $dom1->getElementsByTagName("posts");
            $i = 0;
            foreach($posts_elm as $posts_tag)
            {
                $posts = $posts_tag->getElementsByTagName("post");
                foreach($posts as $post)
                {
                    $id = $post->getAttribute("guid");
                    $fio = $post->getAttribute("fio");
                    $profile = $post->getAttribute("profile");
                    $specialityname = $post->getAttribute("specialityname");
                    $spacialityid = $post->getAttribute("spacialityid");
                    $state = $post->getAttribute("state");
                    $division = $post->getAttribute("division");
                    $post_item = $this->add($id, $fio, $profile, $specialityname, $spacialityid, $state, $division);
                    $workdates = $post->getElementsByTagName("workdate");
                    foreach($workdates as $workdate)
                    {
                        $date = $workdate->getAttribute("date");
                        $timebegin = $workdate->getAttribute("timebegin");
                        $timeend = $workdate->getAttribute("timeend");
                        $place = $workdate->getAttribute("place");
                        $schedule = new workdate_class($date, $timebegin, $timeend, $place);
                        $tickets = $workdate->getElementsByTagName("ticket");
                        foreach($tickets as $ticket)
                        {
                            $id = $ticket->getAttribute("guid");
                            $time = $ticket->getAttribute("time");
                            $status = $ticket->getAttribute("status");
                            $schedule->tickets[] = new ticket_class($id, $time, $status);
                        }
                        $post_item->workdates[] = $schedule;
                    }
                }
            }
        }
    }
?>